﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

namespace MobileUI.MasterPage
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        SqlConnection conn;
        SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["MobileConnectionString"].ConnectionString);
        }

        protected void btLogin_Click(object sender, EventArgs e)
        {
            try
            {

                conn.Open();
                cmd = new SqlCommand()
                {
                    Connection = conn,
                    CommandType = CommandType.Text,
                    CommandText = "Select * from Customer Where UserName = @UserName and Password = @Password"
                };
                cmd.Parameters.Add(new SqlParameter("@UserName", txtUsername.Value.Trim()));
                cmd.Parameters.Add(new SqlParameter("@Password", txtPassword.Value.Trim()));
                var result = cmd.ExecuteReader();
                if (result.Read())
                {
                    Session["CID"] = result["Id"];
                    Session["UID"] = result["UserName"];
                    conn.Close();
                }
                else
                {
                    Response.Write("<script>alert('Wrong Username or password!');window.location = '../Default.aspx';</script>");
                }
            }
            catch (Exception)
            {
                Response.Write("<script>alert('Wrong Username or password!');window.location = '../Default.aspx';</script>");
            }
        }

        protected void btnLogout_Click1(object sender, EventArgs e)
        {
            Session["CID"] = null;
            Session["UID"] = null;
            Response.Redirect("~/Default.aspx");
        }
    }
}